<template>
  <div class="all">

    <el-card>
      <el-row type="flex" justify="end">
        <el-button size="small" type="primary" class="addBtn" @click="addPerm('0','1')">添加权限</el-button>
      </el-row>
      <el-table :data="tableData" row-key="id" height="500" border style="width: 100%">
        <el-table-column prop="title" label="权限名称" width="180" />
        <el-table-column prop="code" label="权限标识" width="180" />
        <el-table-column prop="description" label="权限描述" />
        <el-table-column label="操作" width="246">
          <template v-slot="{row}">
            <el-button size="small" type="primary" @click="addPerm(row.id,'2',row.pid)">添加子权限</el-button>
            <el-button size="small" @click="editPerm(row.id)">编辑</el-button>
            <el-button size="small" type="danger" @click="delPerm(row.id)">删除</el-button>
          </template>
        </el-table-column>

      </el-table>
      <!-- 分页器 -->
      <!-- <el-row>
        <div class="block">
          <el-pagination layout="prev, pager, next" :total="total" :page-size="pageConfig._limit" :current-page="currentPage" @current-change="pageChange" />
        </div>
      </el-row> -->
    </el-card>
    <permTable :is-show-dialog.sync="isShowDialog" :perm-config="permConfig" :detail-data="detailData" @reloadPage="reloadPage" />
  </div>
</template>

<script>
import { listToTreeData2 } from '@/utils/index'
import { getPermList, delPerm, getPermById, getTotalPerm } from '@/api/permissions'
import permTable from './component/permTable.vue'
export default {
  components: { permTable },
  data() {
    return {
      isShowDialog: false,
      tableData: [],
      // pageConfig: {
      //   _limit: 10,
      //   _start: 0
      // },
      // total: 0,
      // currentPage: 1,
      permConfig: {},
      detailData: {}

    }
  },
  created() {
    this.getPermList()
  },
  methods: {
    async  getPermList() {
      const res = await getPermList()

      // const res = await getPermList()
      this.tableData = await listToTreeData2(res, 0)
      console.log(70, this.tableData)

      const all = await getTotalPerm()
      this.total = all.data
    },
    // 添加权限
    addPerm(pid, type, a) {
      console.log(67, pid, a)
      this.isShowDialog = true
      this.permConfig = {
        pid: `${pid}`,
        type
      }
    },
    // 编辑权限
    async editPerm(id) {
      this.isShowDialog = true
      const res = await getPermById(id)
      this.detailData = res
      console.log(this.detailData)
    },
    // 删除权限
    async  delPerm(id) {
      await this.$confirm('确认要删除吗?')
      await delPerm(id)

      this.$message.success('删除成功')
      this.getPermList()
    },
    reloadPage() {
      this.getPermList()
    }
    // 分页器
    // pageChange(page) {
    //   console.log(84, page)
    //   this.currentPage = page
    //   this.pageConfig._start = page > 1 ? (page - 1) * this.pageConfig._limit : 0
    //   this.getPermList()
    // }

  }
}
</script>

<style lang="scss" scoped>
.all{
  margin: 20px;
}
.addBtn {
  margin-top: 20px;
  margin-bottom: 20px;
}
.block{
  text-align: center;
}
</style>
